package model
{
	import core.Model;
	
	import events.LoginEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.Security;
	
	import global.GameState;
	
	import model.load.StringLoadMessage;
	import model.net.NetSocket;

	public class LoginModel extends EventDispatcher
	{
		private var _model:Model = Model.getInstance();
		private var currentGameServerIp:String;
		private var currentGameServerPort:int;
		private var _currentRoomId:String;
		private var currentResult:int = -1;
		
		public function LoginModel()
		{
			NetSocket.getInstance().addEventListener(Event.CONNECT,onLoginGameServerConnect);
			//NetSocket.getInstance().addEventListener(Event.CLOSE,onLoginGameServerClose);
		}
		
		public function get currentRoomId():String
		{
			return _currentRoomId;
		}

		public function loginAccess():void
		{
			var loginUrl:String = GameState.getServerLink() + "3&snsId=" + GameState.snsId +"&key=" + GameState.snsKey;
			var stringLoadMessge:StringLoadMessage = new StringLoadMessage(onLoginResult);
			stringLoadMessge.url = loginUrl;
			stringLoadMessge.send();
			
			var event:LoginEvent = new LoginEvent(LoginEvent.ACCESSSERVER_LOGINING);
			dispatchEvent(event);
			
		}
		
		public function onLoginResult(result:String):void
		{
			var event:LoginEvent ;
			if (result == "1")
			{
				event = new LoginEvent(LoginEvent.SUCCESS);
			}
			else
			{
				event = new LoginEvent(LoginEvent.FAILED);
			}
			dispatchEvent(event);
		}
		
		public function loginGameServer(roomId:String,serverIp:String,port:int):void
		{
	
			var url:String  = GameState.getServerLink() + "4&roomId="+ roomId + "&key=" + GameState.snsKey;
			var stringLoadMessge:StringLoadMessage = new StringLoadMessage(onLoginGameServerResult);
			stringLoadMessge.url = url;
			stringLoadMessge.send();
			
			_currentRoomId = roomId;
			currentGameServerIp = serverIp;
			currentGameServerPort = port;
			
			var e:LoginEvent = new LoginEvent(LoginEvent.GAMESERVER_LOGINING);
			dispatchEvent(e);
			
			
		}
		
		private function onLoginGameServerResult(result:String):void
		{
			var e:LoginEvent
			if (result == "1")
			{
				_model.traceManager.out("Login GameServer:",currentGameServerIp,currentGameServerPort);
				Security.loadPolicyFile("xmlsocket://" + currentGameServerIp + ":" + currentGameServerPort);
				//_model.traceManager.out("policy load:","xmlsocket://" + currentGameServerIp + ":" + currentGameServerPort);
				NetSocket.getInstance().connect(currentGameServerIp,currentGameServerPort);
				
				
			}
			else if (result == "2")
			{
				e = new LoginEvent(LoginEvent.GAMESERVER_FAILED_RELOGIN);
				dispatchEvent(e);
			}
			else
			{
				e = new LoginEvent(LoginEvent.GAMESERVER_FAILED);
				dispatchEvent(e);
			}
		}
		
		private function onLoginGameServerConnect(event:Event):void
		{
			var e:LoginEvent = new LoginEvent(LoginEvent.GAMESERVER_CONNETED);
			dispatchEvent(e);
			
			
		}
		
		private function onLoginGameServerClose(event:Event):void
		{
			if (currentResult == -1)
			{
				
			}
		}
	}
}